package org.elanfox.intellicommunity.intellicommunityadmin.controller.equipment;

import org.elanfox.intellicommunity.biz.entity.equipment.BaseStationPO;
import org.elanfox.intellicommunity.biz.service.core.equipment.BaseStationService;
import org.elanfox.intellicommunity.common.base.ApiResult;
import org.elanfox.intellicommunity.common.base.ResultCodeEnum;
import org.elanfox.intellicommunity.common.constant.RedisConstant;
import org.elanfox.intellicommunity.intellicommunityadmin.dto.BaseStationDTO;
import org.elanfox.intellicommunity.intellicommunityadmin.service.CmdSendService;
import org.elanfox.intellicommunity.msg.dto.CmdDTO;
import org.elanfox.intellicommunity.msg.ienum.CmdEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/3/20
 */
@RestController
@RequestMapping("/api/device/base")
public class BaseStationController {
    @Autowired
    private CmdSendService cmdSendService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private BaseStationService baseStationService;

    /**
     * 设置低频参数
     *
     * @param stationId
     * @return
     */
    @PostMapping("/set/lfScope")
    @ResponseBody
    public ApiResult setLfScope(Integer stationId, int scope) {
        cmdSendService.lfScope(stationId, scope);

        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    /**
     * 设置433M的RSSI过滤功能
     *
     * @param stationId
     * @param enabled
     * @param limitVal
     * @return
     */
    @PostMapping("/set/rssiFilter")
    public ApiResult setRssiFilter(
            Integer stationId,
            boolean enabled,
            @RequestParam(required = false, defaultValue = "0") int limitVal
    ) {
        cmdSendService.rssiFilter(stationId, enabled, limitVal);

        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    /**
     * 设置蜂鸣器状态
     *
     * @param stationId
     * @param enabled
     * @return
     */
    @PostMapping("/set/buzzerStatus")
    public ApiResult setBuzzerStatus(Integer stationId, boolean enabled) {
        cmdSendService.buzzerStatus(stationId, enabled);

        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    /**
     * 设置设备地址
     *
     * @param stationId
     * @param deviceAddr
     * @return
     */
    @PostMapping("/set/deviceAddr")
    public ApiResult setDeviceAddr(Integer stationId, long deviceAddr) {
        cmdSendService.deviceAddr(stationId, deviceAddr);

        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    /**
     * 设置设备过滤等待时间
     *
     * @param stationId
     * @param interval
     * @return
     */
    @PostMapping("/set/filterTime")
    public ApiResult filterTime(Integer stationId, int interval) {
        cmdSendService.filterTime(stationId, interval);

        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    /**
     * 轮询获取指令结果
     *
     * @param stationId
     * @param cmdCode
     * @return
     */
    @PostMapping("/set/result")
    public ApiResult result(Integer stationId, String cmdCode) {
        BaseStationPO stationPO = baseStationService.findById(stationId);
        String hexAddr = stationPO.getHexAddr();
        CmdDTO cmdDTO = (CmdDTO) redisTemplate.opsForHash().get(RedisConstant.getKeyCmd(hexAddr), cmdCode);

        if (cmdDTO == null) {
            return ApiResult.errorWith(ResultCodeEnum.OPERATION_FAILED, "当前没有该指令");
        }

        if (cmdDTO.getCmdStatus().getStatus() == CmdEnums.Status.FAILED) {
            redisTemplate.opsForHash().delete(RedisConstant.getKeyCmd(hexAddr), cmdCode);
            return ApiResult.errorWith(ResultCodeEnum.OPERATION_FAILED, cmdDTO.getCmdStatus().getMsg());
        }

        if (cmdDTO.getCmdStatus().getStatus() == CmdEnums.Status.HANDLE_SUCCESS) {
            redisTemplate.opsForHash().delete(RedisConstant.getKeyCmd(hexAddr), cmdCode);
            return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
        }

        return ApiResult.resultWith(ResultCodeEnum.IN_EXECUTION);
    }

    @PostMapping("/{stationId}")
    public ApiResult baseStation(@PathVariable("stationId") Integer stationId) {
        BaseStationPO stationPO = baseStationService.findById(stationId);

        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, new BaseStationDTO(stationPO));
    }
}
